<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>编辑备品计划</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="./lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="./css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">

    <!-- 隐藏ID -->
    <input type="hidden" name="id">

    <!-- 退役资产编号 -->
    <div class="layui-form-item">
        <label class="layui-form-label required">退役资产编号</label>
        <div class="layui-input-block">
            <input type="text" name="propertyNumber" lay-verify="required" placeholder="请输入退役资产编号"
                   autocomplete="off" class="layui-input">
        </div>
    </div>

    <!-- 项目编号 -->
    <div class="layui-form-item">
        <label class="layui-form-label required">项目编号</label>
        <div class="layui-input-block">
            <input type="text" name="projectNumber" lay-verify="required" placeholder="请输入项目编号"
                   autocomplete="off" class="layui-input">
        </div>
    </div>

    <!-- 项目名称 -->
    <div class="layui-form-item">
        <label class="layui-form-label required">项目名称</label>
        <div class="layui-input-block">
            <input type="text" name="projectName" lay-verify="required" placeholder="请输入项目名称"
                   autocomplete="off" class="layui-input">
        </div>
    </div>

    <!-- 物料描述 -->
    <div class="layui-form-item">
        <label class="layui-form-label required">物料描述</label>
        <div class="layui-input-block">
            <input type="text" name="description" lay-verify="required" placeholder="请输入物料描述"
                   autocomplete="off" class="layui-input">
        </div>
    </div>

    <!-- 状态 -->
    <div class="layui-form-item">
        <label class="layui-form-label required">状态</label>
        <div class="layui-input-block">
            <select name="status" lay-filter="status">
                <option value="0">未完成</option>
                <option value="1">已完成</option>
            </select>
        </div>
    </div>

    <!-- 提交按钮 -->
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>

<script src="./lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'layer', 'jquery'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.jquery;

        // 接收数据并填充表单
        window.loadData = function (data) {
            $("input[name='id']").val(data.id);
            $("input[name='propertyNumber']").val(data.propertyNumber);
            $("input[name='projectNumber']").val(data.projectNumber);
            $("input[name='projectName']").val(data.projectName);
            $("input[name='description']").val(data.description);
            $("select[name='status']").val(data.status);

            // 渲染下拉框
            form.render('select');
        };

        // 监听提交事件
        form.on('submit(saveBtn)', function (data) {
            var loadingIndex = layer.load(2); // 加载动画

            $.ajax({
                url: '/plan/spare/update',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(data.field),
                success: function (res) {
                    layer.close(loadingIndex);
                    if (res.code === 0) {
                        layer.msg('更新成功');

                        // 关闭弹窗并刷新表格
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                        parent.location.reload();
                    } else {
                        layer.msg('更新失败：' + res.msg);
                    }
                },
                error: function () {
                    layer.close(loadingIndex);
                    layer.msg('网络异常，请稍后再试');
                }
            });

            return false; // 阻止表单跳转
        });
    });
</script>
</body>
</html>
